package cluedao

import (
	"errors"
	"fmt"
	"hft/Database/admin"
	services "hft/Http/Services"
	"hft/e"
	"hft/global"
	"time"

	"gorm.io/gorm"
)

// 判断活动
func IsType(activity_type int, activityID int, db *gorm.DB) (ca interface{}, err error) {
	if activity_type == 0 {
		var activity *admin.CommonActivity

		err = db.First(&activity, activityID).Error

		if err != nil {
			return nil, errors.New(e.ActivityNotFound)
		}
		return activity, nil
	} else if activity_type == 1 {
		var activity *admin.Activity
		err = db.First(&activity, activityID).Error

		if err != nil {
			return nil, errors.New(e.ActivityNotFound)
		}
		return activity, nil
	} else {
		return nil, errors.New(e.ActivityInValid)
	}
}

// 判断是否需要发送验证码
func NeedSendCode(phone string) (err error) {
	exists, err := global.OC_REDIS.Exists(fmt.Sprintf("NOW_USER_%v_EXPIRE", phone)).Result()
	if err != nil {
		return errors.New(e.ErrorInfo)
	}
	if exists != 1 {
		_, err = global.OC_REDIS.Set(fmt.Sprintf("NOW_USER_%v_EXPIRE", phone), "", 24*time.Hour).Result()
		if err != nil {
			return errors.New(e.ErrorInfo)
		}
		err = services.BaseService.SendCode(phone, "USER_")
		if err != nil {
			return err
		}
	}
	return nil
}
